package com.archerdu.apply.controller.admin;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.archerdu.apply.model.Company;
import com.archerdu.apply.model.User;
import com.archerdu.apply.service.CompanyManager;
import com.archerdu.base.controller.BaseController;
import com.archerdu.base.dao.IPage;
import com.archerdu.commons.constant.DataConstant;

@Controller
@Scope("prototype")
@RequestMapping("/admin/company")
public class CompanyController extends BaseController {

    private static final long serialVersionUID = 3298072203679257178L;

    private CompanyManager    companyManager;
    private Company           company;

    @RequestMapping("/main")
    public String main() {
        return "admin/company/main";
    }

    @RequestMapping(value = "/add")
    public ModelAndView add(HttpServletRequest requ, CompanyController mc) {
        ModelAndView mv = new ModelAndView();

        mv.setViewName("admin/company/add");

        if ("post".equalsIgnoreCase(requ.getMethod())) {
            try {
                company.setCreateUser(((User) requ.getSession().getAttribute(DataConstant.SESSION_SYS_USER)).getId());
                company.setCreatetime(getSqlDate());
                companyManager.add(company);
                addMessage(DataConstant.COMMON_ADD_SUCCESS);
            } catch (Exception e) {
                addMessage(DataConstant.COMMON_ADD_FAILED);
                logger.error(e.getMessage(), e);
            }
        }
        mv.addObject(getMessage());
        return mv;
    }

    @RequestMapping(value = "/edit")
    public ModelAndView edit(HttpServletRequest requ, CompanyController mc) {

        logger.debug("company.id is " + mc.getCompany().getId());
        company = mc.getCompany();

        if ("get".equalsIgnoreCase(requ.getMethod())) {
            if (!StringUtils.isEmpty(company.getId())) {
                company = companyManager.get(company.getId());
            }
        } else {
            company.setUpdateUser(((User) requ.getSession().getAttribute((DataConstant.SESSION_SYS_USER))).getId());
            company.setUpdatetime(getSqlDate());
            try {
                companyManager.modify(company);
                company = companyManager.get(company.getId());
                addMessage(DataConstant.COMMON_EDIT_SUCCESS);
            } catch (Exception e) {
                addMessage(DataConstant.COMMON_EDIT_FAILED);
                logger.error(e.getMessage(), e);
            }
        }

        ModelAndView mav = new ModelAndView();
        mav.addObject(getMessage());
        mav.addObject(company).setViewName("admin/company/edit");
        return mav;
    }

    @RequestMapping(value = "/remove")
    public ModelAndView remove(HttpServletRequest requ, CompanyController mc, Integer pageNo, Integer pageSize) {
        company = mc.getCompany();
        if (null != company && null != company.getId()) {
            company = companyManager.get(company.getId());
            company.setUseFlag(DataConstant.COMMON_USE_FLAG_NO);
            try {
                companyManager.modify(company);
                addMessage(DataConstant.COMMON_DEL_SUCCESS);
            } catch (Exception e) {
                addMessage(DataConstant.COMMON_DEL_FAILED);
                logger.error(e.getMessage(), e);
            }
        }
        ModelAndView mav = page(requ, mc, pageNo, pageSize);
        mav.addObject(getMessage());
        return mav;
    }

    @RequestMapping(value = "/page")
    public ModelAndView page(HttpServletRequest requ, CompanyController ac, Integer pageNo, Integer pageSize) {

        if (null == ac.getCompany()) {
            company = new Company();
        } else {
            company = ac.getCompany();
        }

        if (null == pageNo || null == pageSize) {
            pageNo = 1;
            pageSize = 20;
        }
        logger.debug("companyManager is " + companyManager);
        company.setUseFlag(DataConstant.COMMON_USE_FLAG_YES);
        IPage<Company> page = companyManager.pageSearch(company, pageNo, pageSize);

        ModelAndView mav = new ModelAndView();
        mav.addObject(company);
        mav.addObject(page).setViewName("admin/company/page");
        return mav;
    }

    public CompanyManager getCompanyManager() {
        return companyManager;
    }

    public void setCompanyManager(CompanyManager companyManager) {
        this.companyManager = companyManager;
    }

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }
}
